// xc_lshop/index/com/advertise.js
var app = getApp();
var actcouponbill = require('../../../xc/model/coupon_cont')
// const utils = require('../../utils/util.js');
const utils = require('../../../we7/resource/js/util');

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    config: {
      type: Object,
      default: {}
    },
    member: {
      type: Object,
      default: {}
    },
    basicurl: {
      type: String,
      default:'-1'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
     
      var advertisement = this.data.config.advertisement
      if (advertisement && advertisement['status'] == 1) {
        // var updatedata = {};
        var member = this.data.member;
        var uid = member['id'];
        var key = "adv_" + uid;
        if (advertisement['mode'] == 1) {
          // this.popup(key)
          this.getcoupon();
        } else {
          var judge = this.jugetime(key);
          if (judge) {
            // this.popup(key)
            this.getcoupon();
          }
        }
      }
    },
    //获取优惠券
    getcoupon(){
      var that=this;
      actcouponbill.adcoupon().then(res=>{  
   
        if(res["status"]==1){
          var modellist=[];
          if(  res["obj"]){
            modellist=res["obj"];
            var ilen =modellist.length;
            var ids=[];
            for(var i=0;i<ilen;i++){
              ids.push(modellist[i]['id'])
          
              if(modellist[i].cid ==1)
              {
                modellist[i].val= modellist[i].val
              }              
              modellist[i].fullmoney= modellist[i].fullmoney
  
              if(i<2)
              {
                var temtag=""
                if(  modellist[i].hascondition==1){
                
                }
                else
                {
                  temtag+="满" +(modellist[i].fullmoney/100)
                }
  
                if(  modellist[i].cid==1){
                  temtag+="减" +(modellist[i].val/100)
                }
                else
                {
                  temtag+=modellist[i].val/10 +"折"
                }
                
  
              }
             
  
  
            }

          }
          
         
          var idsstr=ids.join(",");
         
  
          that.setData({modellist:modellist,idsstr})

          if(modellist.length>0){
            var member = this.data.member;
            var uid = member['id'];
            var key = "adv_" + uid;
            this.popup(key)
          }
         
        }
      })
    },
    //弹出
    popup(key){
        var updatedata = {};
        updatedata['show']=true;
        updatedata['advertisement']=this.data.config.advertisement;
        updatedata['advstyle']=this.data.config.advertisement.xcstyledata.customStyle;
        
        this.setData(updatedata)
        this.setStorage(key)
        
     

    },
    
    //设置缓存
    setStorage(key){
      var val=this.getendtime();
      wx.setStorageSync(key,val)
    },
    //判断是否可以弹出
    jugetime(key) {
      var judge = false;

      try {
        var value = wx.getStorageSync(key)
        if (value) {
          var now = new Date();
          var nowtime = Date.parse(now);
          if (value < nowtime) {
            judge = true
          }
        } else {
          judge = true
        }
      } catch (e) {

      }

      return judge;

    },
    //今日过期时间
    getendtime() {

      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      var ymd = year + "-" + month + "-" + date + " 23:59:59"
   
      var today = new Date(ymd);
      var ttime = Date.parse(today);
      return ttime
    },

    
    onClickHide(){
      this.setData({
        show:false
      })
    },
    receivecoupon(ev){
      var dataset=ev.currentTarget.dataset;
      var item=  dataset.item 
      var idx= dataset.idx
      var that = this
      var app =getApp();
  
      app.xc_sys_config.authop.oppower("login").then(function (res) {
       
        actcouponbill.receivecoupon(item.id).then(res=>{
          var updatemodel={}
          var keyval="modellist["+idx+"]."
          if(res["status"]==1){     
             updatemodel[keyval+"myreceivednum"]=res["obj"].myreceivednum
             updatemodel[keyval+"receivednum"]=res["obj"].receivednum
             
             
            that.setData(updatemodel)
           
            wx.showToast({
              title: '领取成功',
            })
          }
          else
          {
            if( res["status"]==-1){
              wx.showToast({
                title: res["message"],
              })
            }else if ( res["status"]==2){
              updatemodel[keyval+"receivednum"]=this.data.modellist[idx].num;
              that.setData(updatemodel)
            }
            else if ( res["status"]==3){
              updatemodel[keyval+"myreceivednum"]=this.data.modellist[idx].everyonelimit
              that.setData(updatemodel)
            }
    
          }
    
        })
  
  
      })
      
     
     
  
  
    },
     //一键领取
    getallcoupon(ev){
    
      var dataset=ev.currentTarget.dataset;
      var ids=  dataset.ids 
      var idx= dataset.idx
      var that = this
      var app =getApp();
  
      app.xc_sys_config.authop.oppower("login").then(function (res) {
       
        actcouponbill.receiveallcoupon(ids).then(res=>{
          var updatemodel={}
          // var keyval="modellist["+idx+"]."
          if(res["status"]==1){  
            if(res["obj"]>0){
              wx.showToast({
                title: '成功领取'+ res["obj"] +'张',
              })
            }  else{
              wx.showToast({
                title: '已达领取上限',
              })
            }
             that.onClickHide()
          }
          else
          {
            wx.showToast({
              title: '领取失败',
            })
    
          }
    
        })
  
  
      })
      
     
     
  
  
    },

    
   
  }
})